package com.wangjiyuan.shiwupai.ui_food.food;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.base.base_map.BaseMap;
import com.wangjiyuan.shiwupai.bean.first_food.FoodBean;
import com.wangjiyuan.shiwupai.ui_food.food.adapter.FoodRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * 食物百科的画面
 */
public class FoodFragment extends Fragment implements FoodContract.IFoodView{
    private Context mContext;
    private RecyclerView recyclerView_food;
    private ProgressBar progress_food;

    private List<Object> groupBeanList;
    private FoodRecyclerAdapter foodRecyclerAdapter;
    private LinearLayoutManager linearLayoutManager;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext=context;
        groupBeanList=new ArrayList<>();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FoodPresenter foodPresenter=new FoodPresenter(this);
        Map<String, String> foodMap = BaseMap.baseMap();
        foodPresenter.getFoodObservable(foodMap);
        foodRecyclerAdapter=new FoodRecyclerAdapter(mContext,groupBeanList);
        linearLayoutManager=new LinearLayoutManager(mContext, OrientationHelper.VERTICAL,false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_food, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView_food= (RecyclerView) view.findViewById(R.id.recyclerView_food);
        progress_food= (ProgressBar) view.findViewById(R.id.progress_food);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView_food.setHasFixedSize(true);
        recyclerView_food.setLayoutManager(linearLayoutManager);
        recyclerView_food.setAdapter(foodRecyclerAdapter);
    }

    @Override
    public void showProgress() {
//        progress_food.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progress_food.setVisibility(View.GONE);
    }

    @Override
    public void loadData(FoodBean foodBean) {
        List<FoodBean.GroupBean> group = foodBean.getGroup();
        groupBeanList.addAll(group);
//        第一条数据是头布局
        groupBeanList.add(0,true);
        foodRecyclerAdapter.notifyDataSetChanged();
    }
}
